﻿using System;
using System.Windows.Controls;
using LukasKopenec.LocalizationSupport;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that changes the size of an active <see cref="ViewModel.Elements.Page"/>.
    /// </summary>
    public class SetPageSizeCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="SetPageSizeCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// A reference to the main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public SetPageSizeCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Set Page Size";
            ScreenTipText = "Changes the size of the current page.";

            mainWindow.DocumentPane.SelectionChanged += new SelectionChangedEventHandler(DocumentPane_SelectionChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.DocumentPane.SelectedItem is PagePanel;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var page = ApplicationWindow.ActivePage;

            if (parameter == null)
                ApplicationWindow.PageAppearanceLauncherCmd.Execute(null);
            else
            {
                var sizeString = parameter.ToString();
                var sizes = sizeString.Split('x');
                if (sizes.Length != 2)
                    throw new FormatException("The parameter has not a correct format");

                double width = double.Parse(sizes[0]) / 10;
                double height = double.Parse(sizes[1]) / 10;

                var cmd = new ViewModel.SetterCommand<double, double>(page.UndoStack, page.RedoStack)
                {
                    Target = page,
                    Property1 = "Width",
                    Property2 = "Height",
                    Value1 = width,
                    Value2 = height,
                    Name = (string)LocalizationManager.GetValue("Commands.Names.SetSize")
                };
                cmd.Execute(null);
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the active document has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void DocumentPane_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RaiseCanExecuteChanged();
        }

        #endregion
    }
}
